﻿Imports Microsoft.VisualBasic
Imports System.Data.SqlClient
Imports System.Data
Imports System.Collections.Generic

Namespace Pic
    Public Class Users
        Inherits clsBase
#Region "Private Variables"
        Private _Email As String
        Private _Name As String
        Private _Surname As String
        Private _Photo As String
        Private _Created As DateTime
        Private _Modified As DateTime
#End Region
#Region "Properties"
        Public Property Email() As String
            Get
                Return _Email
            End Get
            Set(ByVal value As String)
                _Email = value
            End Set
        End Property
        Public Property Name() As String
            Get
                Return _Name
            End Get
            Set(ByVal value As String)
                _Name = value
            End Set
        End Property
        Public Property Surname() As String
            Get
                Return _Surname
            End Get
            Set(ByVal value As String)
                _Surname = value
            End Set
        End Property
        Public Property Photo() As String
            Get
                Return _Photo
            End Get
            Set(ByVal value As String)
                _Photo = value
            End Set
        End Property
        Public Property Created() As DateTime
            Get
                Return _Created
            End Get
            Set(ByVal value As DateTime)
                _Created = value
            End Set
        End Property
        Public Property Modified() As DateTime
            Get
                Return _Modified
            End Get
            Set(ByVal value As DateTime)
                _Modified = value
            End Set
        End Property






#End Region
#Region "Constructors"
        Public Sub New(ByVal Email_ As String, ByVal Name_ As String, ByVal Surname_ As String, ByVal Photo_ As String, ByVal Created_ As DateTime, ByVal Modified_ As DateTime)
            Me.Email = Email_
            Me.Name = Name_
            Me.Surname = Surname_
            Me.Photo = Photo_
            Me.Created = Created_
            Me.Modified = Modified_
        End Sub
#End Region
#Region "Methods"
        Private Shared Function GetUsersFromReader(ByVal reader_ As IDataReader) As Users
            Dim rtrn As Users = New Users(reader_("Email"), reader_("Name"), reader_("Surname"), reader_("Photo"), reader_("Created"), IIf(IsDBNull(reader_("Modified")), "", reader_("Modified")))
            Return rtrn
        End Function

        Public Shared Function GetUserByEmail(ByVal Email_ As String) As Users
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "Select * from tbl_Users where Email=@Email"
            Dim reader As IDataReader
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = Email_
                reader = ExecuteReader(cmd, CommandBehavior.SingleRow)
                If reader.Read Then
                    Return GetUsersFromReader(reader)
                Else
                    Return Nothing
                End If
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function

        Private Shared Function GetUsersListFromReader(ByVal reader As IDataReader) As List(Of Users)
            Dim rtrnList As New List(Of Users)
            Do While reader.Read
                rtrnList.Add(GetUsersFromReader(reader))
            Loop
            Return rtrnList
        End Function
        Public Shared Function GetUsersListByFilter(Optional ByVal Filtre_ As String = "", Optional ByVal FiltreOrder_ As String = "") As List(Of Users)
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "select * from website1.dbo.tbl_Users " & Filtre_ & " " & FiltreOrder_
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text
                Return GetUsersListFromReader(ExecuteReader(cmd, CommandBehavior.Default))
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function
        Public Shared Function InsertUser(ByVal Email_ As String, ByVal Name_ As String, ByVal Surname_ As String, ByVal Photo_ As String) As Integer
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "insert into tbl_Users(email,name,surname,photo,created,modified) values(@Email,@Name,@Surname,@Photo,@created,@modified)"
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = Email_
                cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name_
                cmd.Parameters.Add("@Surname", SqlDbType.VarChar).Value = Surname_
                cmd.Parameters.Add("@Photo", SqlDbType.VarChar).Value = Photo_
                cmd.Parameters.Add("@created", SqlDbType.DateTime).Value = Now.Date
                cmd.Parameters.Add("@modified", SqlDbType.DateTime).Value = Now.Date
                Return ExecuteNonQuery(cmd)
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function
        Public Shared Function UpdateUser(ByVal Email_ As String, ByVal Name_ As String, ByVal Surname_ As String, ByVal Photo_ As String) As Integer
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "update tbl_Users set Name=@Name, Surname=@Surname, photo=@Photo, user_email=@UserEmail, Modified=@modified where email=@Email"
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = Email_
                cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name_
                cmd.Parameters.Add("@Surname", SqlDbType.VarChar).Value = Surname_
                cmd.Parameters.Add("@Photo", SqlDbType.VarChar).Value = Photo_
                cmd.Parameters.Add("@modified", SqlDbType.DateTime).Value = Now.Date
                Return ExecuteNonQuery(cmd)
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function
        Public Shared Function DeleteUser(ByVal Email_ As String) As Boolean
            Dim conn As New SqlConnection(ConnectionString)
            Dim veriString As String = "Select count(1) from tbl_Users where email=@Email"
            Dim count As Integer
            Try
                conn.Open()
                Dim cmd As New SqlCommand(veriString, conn)
                cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = Email_
                count = ExecuteScalar(cmd)
                If count = 0 Then
                    Return False
                Else
                    cmd.CommandText = "Delete from tbl_Users where email=@Email"
                    cmd.CommandType = CommandType.Text
                    cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = Email_
                    Return ExecuteNonQuery(cmd)

                End If
            Catch ex As Exception
                Throw ex
            Finally
                conn.Close()
            End Try
        End Function

        'Public Shared Function SilinebilirMi(ByVal ProjeID_ As Integer) As Boolean
        '    Dim conn As New SqlConnection(ConnectionString)
        '    Dim veriString As String = "Select count(1) from tblSondajlar where ProjeID=" & ProjeID_
        '    Dim count As Integer
        '    Try
        '        conn.Open()
        '        Dim cmd As New SqlCommand(veriString, conn)
        '        count = ExecuteScalar(cmd)
        '        If count = 0 Then
        '            Return True
        '        Else
        '            Return False
        '        End If
        '    Catch ex As Exception
        '        Throw ex
        '    Finally
        '        conn.Close()
        '    End Try
        'End Function
#End Region
    End Class
End Namespace

